﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Collections.Generic;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Config;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;

public partial class Theme_3_UserControls_OtherCourseResource : System.Web.UI.UserControl
{
    ManagerPaper opPaper = new ManagerPaper();
    NewProjectPlan opProjectPlan = new NewProjectPlan();

    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    //string _descriptionPaper = "试卷得分:";
    //string _descriptionResource = "共学习了{0}次,时长{1}秒";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        BinderCourseResource(ViewState["CourseID"].ToString());
    }
   


    private void BinderCourseResource(string cid)
    {
        //string search = "select * from TResource where ResouceID in (select )";

        string search = "FKCourseresCourseID =" + cid;
        DataTable dt = opCourse.QueryCourseResource(search);
        List<CourseResourceInfo> list = new List<CourseResourceInfo>();
        //if (ViewState["ProjectID"].ToString() != "0")
        //{
        //    list = FillListResource(dt);
        //}
        //else
        //{
            list = FillList(dt);
        //}

        int count = list.Count;
        if (count > 10)
        {
            if (!PowerCreator.LMS.SCR.Dog.CheckDog())
            {
                list.RemoveRange(10, count - 10);

            }
        }


        rpt_OtherCourseResource.DataSource = list;
        rpt_OtherCourseResource.DataBind();

    }




    protected void rpt_ResourceList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            //打开看看
            if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
            {
                PowerCreator.LMS.Common.Script.Script.Alert(config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }
            VOTCourseResource vo = opCourse.LoadCourseResource(int.Parse(e.CommandArgument.ToString()));
            Label lbl_msg = (Label)e.Item.FindControl("lbl_resourcetype_paper");
            if (vo != null)
            {

                VOTResource voResource = opCourse.LoadResource(vo.FKCoursersResourceID);
                if (voResource == null)
                {
                    lbl_msg.Text = config.GetLanagerValueByKey("ResourceList_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page);
                    return;
                }
                if (ConfigurationManager.AppSettings["PubliceIsLogin"].ToString() == "1")
                {
                    if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
                    {
                        Script.Alert( config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                        return;

                    }
                }
                if (vo.CoursersClass == 1)
                {
                    //Script.NewOpenURL(WebUtils.WebRoot + voResource.ResourceUrl + voResource.ResourceEntry, "testfileurl");
                    string _fileUrl = WebUtils.WebRoot + voResource.ResourceUrl + voResource.ResourceEntry;
                    //string path = Server.MapPath(_fileUrl);

                    if (voResource.ResourceClass != 3)
                    {

                        if (!voResource.ResourceMmsUpdateState)//检查课件是否是第一次被点播，如果是第一次被点播，则修改remoteclip.asx文件以及读取content.xml文件，修改Tgrade表中的相关字段（针对直播后载入新课件以期后期评论）。
                        {

                            //PowerCreator.LMS.Web.Course.Resource opResource = new Resource();
                            //VOTCourseResource voCourseResource = opCourse.LoadCourseResource(int.Parse(ViewState["CourseResourceID"].ToString()));
                            if (vo.CourseresID > 0)
                            {
                                VOTOCourse voCourse = opCourse.LoadVOTCourse(vo.FKCourseresCourseID);
                                if (voCourse.Course_Id > 0)
                                {
                                    opResource.UpdataResourceByEditMMS(voResource);
                                }
                            }

                        }

                        if (PowerCreator.LMS.Web.Config.WebConfig.IsUseWeGe)
                        {
                            if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
                            {
                                Script.Alert(config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                                return;
                            }

                        }
                    }

                    voResource.ResourceClickNum += 1;
                    voResource.ResourceLastVisitTime = DateTime.Now;
                    if (!opCourse.UpdateResource(voResource))
                    {
                        //lab_err.Text = config.GetLanagerValueByKey("theme_PageCode_ErrmMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
                    }
                    if (ViewState["ProjectID"].ToString() == "0")
                    {
                        Script.NewOpenURL("OpenCourseware.aspx?ResourceID=" + voResource.ResourceID.ToString() + "&CourseResourceID=" + vo.CourseresID.ToString());
                    }
                    else
                    {
                        string url = "../../web/Project/OpenPowercreator.aspx?ProjectID=" + ViewState["ProjectID"].ToString() + "&ProjectClassID=" + ViewState["ProjectClassID"].ToString() + "&ProjectResourceID=" + ViewState["ProjectResourceID"].ToString() + "&CourseResourceID=" + vo.CourseresID.ToString() + "&ResourceID=" + vo.FKCoursersResourceID.ToString();
                        Script.NewOpenURL(url, "open");
                    }


                }
                else
                {

                    PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[WebConfig.SESSION_USER_USERINFO];
                    if (login == null)
                    {
                        Script.Alert(config.GetLanagerValueByKey("theme_Course_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                        return;
                    }


                    PowerCreator.LMS.Web.Paper.ManagerPaper objManagerPaper = new ManagerPaper();
                    string search = " FKUPHistoryUserID=" + login.UserID.ToString() + " and FKUPHistoryCourseID=" + vo.FKCourseresCourseID.ToString() + " and FKUPHistoryPaperID=" + vo.FKCoursersPaperID.ToString();

                    DataTable dt_UserPaperHistory = objManagerPaper.QueryUserPaperHistory(search);
                    if (dt_UserPaperHistory.Rows.Count != 0)
                    {
                        string readState = dt_UserPaperHistory.Rows[0]["UPHistoryReadState"].ToString();
                        string SubmitTime = dt_UserPaperHistory.Rows[0]["UPHistorySubmitTime"].ToString().Trim();
                        if (readState == "1")
                        {
                            Script.NewOpenURL(WebUtils.WebRoot + "/web/UserObject/ReadedPaper.aspx?PaperID=" + vo.FKCoursersPaperID.ToString() + "&ProjectID=0&ClassID=0&CourseID=" + vo.FKCourseresCourseID.ToString() + "&UserID=" + login.UserID.ToString());
                        }
                        else if (readState == "0" && SubmitTime == "")
                        {
                            Script.NewOpenURL(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ResourceID=" + vo.FKCoursersPaperID.ToString() + "&ProjectID=0&ProjectClassID=0&IsBuKao=0&ProjectResourceID=0&CourseResourceID=0&CourseID=" + vo.FKCourseresCourseID.ToString());
                        }
                        else
                        {
                            lbl_msg.Text = config.GetLanagerValueByKey("theme_Paper_ExamMsg", PowerCreator.LMS.Language.LanagetItemType.Page);

                        }

                    }
                    else
                    {
                        Script.NewOpenURL(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ResourceID=" + vo.FKCoursersPaperID.ToString() + "&ProjectID=0&ProjectClassID=0&IsBuKao=0&ProjectResourceID=0&CourseResourceID=0&CourseID=" + vo.FKCourseresCourseID.ToString());
                    }



                }

            }
            else
            {
                lbl_msg.Text = config.GetLanagerValueByKey("ResourceList_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
    }



    //DataTable为多表联链接查询后的数据
    private List<CourseResourceInfo> FillList(DataTable dt)
    {

        List<CourseResourceInfo> list = new List<CourseResourceInfo>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["FKCoursersResourceID"].ToString() != ViewState["ResourceID"].ToString())
            {
                CourseResourceInfo item = new CourseResourceInfo();
                item.CourseID = int.Parse(dt.Rows[i]["FKCourseresCourseID"].ToString());
                item.CourseresID = int.Parse(dt.Rows[i]["CourseresID"].ToString());
                item.CoursersRestpID = int.Parse(dt.Rows[i]["FKCoursersRestpID"].ToString());


                VOTResourceType voResourceType = opCourse.LoadResourceType(item.CoursersRestpID);
                //item.CoursersRestpName = voResourceType.RestpName;
                item.CoursersClassID = int.Parse(dt.Rows[i]["CoursersClass"].ToString());
                //item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));

                if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Courseware))  //课件资源
                {
                    item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersResourceID"].ToString());
                    VOTResource voResource = opCourse.LoadResource(item.CoursersResourceID);
                    item.ResourceName = voResource.ResourceName;
                    //item.CoursersResourceAuthor = voResource.ResourceAuthor;
                    //item.CoursersResourceDescription = voResource.ResourceDescription;
                    item.CoursersResourceUrl = GetPath(voResource.ResourceUrl, voResource.ResourceClass);
                    item.CoursersResourceImageUrl = item.CoursersResourceUrl + GetPicName(voResource.ResourceEntry, voResource.ResourceClass);
                }
                else if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Paper)) //课程资源
                {
                    item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersPaperID"].ToString());
                    VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                    //item.ResourceName = voPaper.PaperName;
                    //item.CoursersResourceDescription = voPaper.PaperName;
                    item.CoursersResourceUrl = "";
                    item.CoursersResourceImageUrl = "images/gkkpic.jpg";
                }

                list.Add(item);
            }
        }
        return list;
    }

    private string GetPicName(string fileName, int Resourceclass)
    {
        string rt = "images/gkkpic.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if (Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }
}
